package cn.b.sky.mes.mm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.mm.dao.UserWarehouseDao;
import cn.b.sky.mes.mm.model.Warehouse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.mm.model.UserWarehouse;
import cn.b.sky.mes.mm.model.condition.UserWarehouseCondition;
import cn.b.sky.mes.mm.service.UserWarehouseService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("userWarehouseService")
public class UserWarehouseServiceImpl implements UserWarehouseService {

  private static final Logger LOG = LoggerFactory.getLogger(UserWarehouseServiceImpl.class);

  @Resource
  private UserWarehouseDao userWarehouseDao;

  @Override
  public List<UserWarehouse> selectUserWarehouseListByCondition(UserWarehouseCondition condition) {
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return userWarehouseDao.selectUserWarehouseListByCondition(condition);
  }

  @Override
  public int selectUserWarehouseCountByCondition(UserWarehouseCondition condition) {
    return userWarehouseDao.selectUserWarehouseCountByCondition(condition);
  }

  @Override
  public void insertUserWarehouse(String[] mtCodes, UserWarehouse userWarehouse) {
    if (validateKeyNotNull(userWarehouse)) {
      for (String mtCode : mtCodes) {
        userWarehouse.setMoveTypeCode(mtCode);
        userWarehouseDao.insertUserWarehouse(userWarehouse);
      }
    }
    return;
  }

  @Override
  public void updateUserWarehouse(String[] mtCodes, UserWarehouse userWarehouse) {
    if (!validateKeyNotNull(userWarehouse)) {
      return;
    }
    userWarehouseDao.deleteUserWarehouse(userWarehouse.getUserCode(), userWarehouse.getWarehouseCode(),
        userWarehouse.getSiteCode());
    for (String mtCode : mtCodes) {
      userWarehouse.setMoveTypeCode(mtCode);
      userWarehouseDao.insertUserWarehouse(userWarehouse);
    }
  }

  @Override
  public BatchJobReport deleteUserWarehouse(String userCode, String warehouseCode, String siteCode) {
    String languageCode = SecurityContextUtil.getLanguageCode();
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(warehouseCode)) {
      return new BatchJobReport("", "siteCode ,moveTypeCode,warehouseCode ,userCode is contains null", false,
          languageCode, SecurityContextUtil.getSiteCode());
    }
    String message = warehouseCode;
    boolean success = true;
    String cause = "";
    try {
      userWarehouseDao.deleteUserWarehouse(userCode, warehouseCode, siteCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      success = false;
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError(e.getMessage())) {
        //外键删除已存在提示
        errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode, "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode, "notDelete.M");
      }
      cause = errorMsg;
    }
    return new BatchJobReport(message, cause, success, languageCode, SecurityContextUtil.getSiteCode());
  }

  @Override
  public List<UserWarehouse> getUserWarehouse(String userCode, String warehouseCode, String siteCode) {
    String[] str = { userCode, warehouseCode, siteCode };
    if (validateStringNotNull(str)) {
      return userWarehouseDao.getUserWarehouse(userCode, warehouseCode, siteCode);
    }
    return null;
  }

  private static boolean validateKeyNotNull(UserWarehouse userWarehouse) {
    String[] str = { userWarehouse.getSiteCode(), userWarehouse.getUserCode(), userWarehouse.getWarehouseCode() };
    return validateStringNotNull(str);
  }

  private static boolean validateStringNotNull(String[] str) {
    for (String s : str) {
      if (StringUtils.isBlank(s)) {
        return false;
      }
    }
    return true;
  }

  @Override
  public List<Warehouse> getAssignedWarehouse(String userCode, String siteCode) {
    if (StringUtils.isBlank(userCode) || StringUtils.isBlank(userCode)) {
      return null;
    }
    return userWarehouseDao.getAssignedWarehouse(userCode, siteCode);
  }

  @Override
  public void deleteUserWarehouses(UserWarehouse userWarehouse) {
    if (StringUtils.isBlank(userWarehouse.getUserCode()) || StringUtils.isBlank(userWarehouse.getSiteCode())
        || StringUtils.isBlank(userWarehouse.getWarehouseCode())) {
      return;
    }
    userWarehouseDao.deleteUserWarehouse(userWarehouse.getUserCode(), userWarehouse.getWarehouseCode(),
        userWarehouse.getSiteCode());
  }

  @Override
  public List<UserWarehouse> selectUserWarehouseByMoveType(String moveTypeCode, String siteCode) {
    return userWarehouseDao.selectUserWarehouseByMoveType(moveTypeCode, siteCode);
  }

}
